.TH std::type_identity 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::type_identity \- std::type_identity

.SH Synopsis
   Defined in header <type_traits>
   template< class T >              \fI(since C++20)\fP
   struct type_identity;

   Provides the member typedef type that names T (i.e., the identity transformation).

   If the program adds specializations for std::type_identity, the behavior is
   undefined.

.SH Member types

   Name Definition
   type T

.SH Helper types

   template< class T >                              \fI(since C++20)\fP
   using type_identity_t = type_identity<T>::type;

.SH Possible implementation

   template<class T>
   struct type_identity { using type = T; };

.SH Notes

   std::type_identity can be used to establish non-deduced contexts in template
   argument deduction.

     Feature-test macro     Value    Std        Feature
   __cpp_lib_type_identity 201806L (C++20) std::type_identity

.SH Example


// Run this code

 #include <iostream>
 #include <type_traits>

 template<class T>
 T foo(T a, T b) { return a + b; }

 template<class T>
 T bar(T a, std::type_identity_t<T> b) { return a + b; }

 int main()
 {
     // foo(4.2, 1); // error, deduced conflicting types for 'T'
     std::cout << bar(4.2, 1) << '\\n';  // OK, calls bar<double>
 }

.SH Output:

 5.2

.SH See also

   identity function object that returns its argument unchanged
   (C++20)  \fI(class)\fP
